package com.mornd.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mornd.gulimall.product.entity.SpuInfoDesc;
import com.mornd.gulimall.product.mapper.SpuInfoDescMapper;
import com.mornd.gulimall.product.service.ISpuInfoDescService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * spu信息介绍 服务实现类
 * </p>
 *
 * @author mornd
 * @since 2023-07-23
 */
@Service
public class SpuInfoDescServiceImpl extends ServiceImpl<SpuInfoDescMapper, SpuInfoDesc> implements ISpuInfoDescService {

    /**
     * 保存商品描述图片
     *
     * @param id
     * @param descImgs
     */
    @Override
    // saveSpu() 方法标注了 @Transactional 注解，这里不用标注 @Transactional 事务也能回滚
    //@Transactional(rollbackFor = Exception.class)
    public void saveSpuDescImgs(Long id, List<String> descImgs) {
        String filter = String.join(",", descImgs);
        SpuInfoDesc infoDesc = new SpuInfoDesc();
        infoDesc.setSpuId(id);
        infoDesc.setDecript(filter);
        super.save(infoDesc);

        // saveSpu()标注了@Transactional，此方法不标注@Transactional，这里抛出异常super.save()也可以回滚
        //int a = 1 / 0;
    }

    @Override
    public Optional<SpuInfoDesc> getBySpuId(Long spuId) {
        SpuInfoDesc desc = super.getOne(Wrappers.<SpuInfoDesc>lambdaUpdate().eq(SpuInfoDesc::getSpuId, spuId));
        return Optional.ofNullable(desc);
    }
}
